package com.project.tool.test.spring;

import org.junit.Assert;
import org.junit.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.support.GenericXmlContextLoader;

/**
* spring测试基类,需求指定applicationContext.xml
*/
public class SpringTestCase extends Assert {

	protected Logger log = LoggerFactory.getLogger(getClass());
	private static final String DEFAULT_CONTEXT_LOCATION = "classpath*:spring/**";
	protected static ApplicationContext applicationContext;

	@Before
	public void initApplicationContext() throws Exception {
		//填充用户实例,默认为用户admin1		
		if (applicationContext == null) {
			GenericXmlContextLoader xmlContextLoader = new GenericXmlContextLoader();
			applicationContext = xmlContextLoader.loadContext(getContextLocations());
		}
	}

	protected String getContextLocations() {
		return DEFAULT_CONTEXT_LOCATION;
	}

	@SuppressWarnings("unchecked")
	public static <X> X getBean(String beanName) {
		return (X) applicationContext.getBean(beanName);
	}
}
